package jike.demo16.code01;

import jike.demo16.AlertRule;
import jike.demo16.Notification;
import jike.demo16.NotificationEmergencyLevel;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 监控告警系统类, 当接口的 TPS 超过某个预
 * 先设置的最大值时，以及当接口请求出错数大于某个最大允许值时，就会触发告警，通知接
 * 口的相关负责人或者团队。
 * @date 2022/11/8 22:43
 */
public class Alert {
    private static final String URGENCY = "系统很紧急";

    private final AlertRule rule;

    private final Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public void check(String api,long requestCount,long errorCount, long durationOfSeconds){
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.SERVICE.toString(), "准备报警。。。");
        }

        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.URGENCY.toString(), "准备报警。。。");
        }

    }

}
